version: '3.8'

services:

  postgres:
    image: postgres
    container_name: ${PG_CONTAINER_NAME}
    restart: always
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "5432:5432"
    volumes:
      - ./pg_data:/var/lib/postgresql/data
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    command: >
      postgres -c logging_collector=on
               -c log_directory=/var/log/postgresql
               -c log_destination=stderr
    env_file:
      - .env_docker
    networks:
      - network

  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
      PGADMIN_LISTEN_PORT: ${PGADMIN_LISTEN_PORT}
    ports:
      - "15432:${PGADMIN_LISTEN_PORT}"
    depends_on:
      - postgres
    env_file:
      - .env_docker
    networks:
      - network

  frontend:
    image: nginx
    container_name: web
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ../../frontend/static:/usr/share/nginx/html/
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - backend
    networks:
      - network

  backend:
    build:
      context: .
      dockerfile: ./app/Dockerfile
    command: > 
      sh -c "alembic upgrade head
      && gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind=0.0.0.0:5500"
#      && uvicorn main:app --reload --host 0.0.0.0 --port 5500"
    depends_on:
      - postgres
    container_name: microblog_app
    restart: unless-stopped
    stop_signal: SIGKILL
    ports:
      - 5500:5500
    env_file:
      - .env_docker
    volumes:
      - ./app/media:/microblog_app/app/media
    networks:
      - network

networks:
  network:
    driver: bridge

volumes:
  postgres:
  pgadmin:
  frontend:
